//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.未分类.demo60AND80;

public class demo_67 {
    public demo_67() {
    }

    public static void main(String[] args) {
        String a = "100";
        String b = "110010";
        System.out.println(addBinary(a, b));
    }

    public static String addBinary(String a, String b) {
        StringBuilder sb = new StringBuilder();
        int aIndex = a.length();
        int bIndex = b.length();
        int next = 0;

        while(aIndex > 0 && bIndex > 0) {
            --aIndex;
            --bIndex;
            if (next == 0) {
                if (a.charAt(aIndex) == '1' && b.charAt(bIndex) == '1') {
                    sb.insert(0, '0');
                    next = 1;
                } else if (a.charAt(aIndex) == '0' && b.charAt(bIndex) == '0') {
                    sb.insert(0, '0');
                } else {
                    sb.insert(0, '1');
                }
            } else if (a.charAt(aIndex) == '1' && b.charAt(bIndex) == '1') {
                sb.insert(0, '1');
            } else if (a.charAt(aIndex) == '0' && b.charAt(bIndex) == '0') {
                sb.insert(0, '1');
                next = 0;
            } else {
                sb.insert(0, '0');
            }
        }

        while(aIndex > 0) {
            --aIndex;
            if (next == 1 && a.charAt(aIndex) == '1') {
                sb.insert(0, '0');
            } else if (next == 1 && a.charAt(aIndex) == '0') {
                sb.insert(0, '1');
                next = 0;
            } else {
                sb.insert(0, a.charAt(aIndex));
            }
        }

        while(bIndex > 0) {
            --bIndex;
            if (next == 1 && b.charAt(bIndex) == '1') {
                sb.insert(0, '0');
            } else if (next == 1 && b.charAt(bIndex) == '0') {
                sb.insert(0, '1');
                next = 0;
            } else {
                sb.insert(0, b.charAt(bIndex));
            }
        }

        if (next == 1) {
            sb.insert(0, '1');
        }

        return sb.toString();
    }
}
